import { store } from "@/store";
import { defineStore } from "pinia";
import MerchantAgentAPI, {
  type MerchantAgentListQuery,
  type MerchantAgentItemType,
  type MerchantAgentTransactionListQuery,
  type MerchantAgentTransactionListItem,
} from "@/api/merchant/agent";

export const useMerchantAgentStore = defineStore("merchant-agent", () => {
  const list = ref<MerchantAgentItemType[]>([]);

  const transactionList = ref<MerchantAgentTransactionListItem[]>([]);

  const totalCount = ref(0);

  const totalCount2 = ref(0);

  const totalPage = ref(0);

  const totalPage2 = ref(0);

  const loading = reactive({
    list: false,
    create: false,
    update: false,
    delete: false,
    transactionList: false,
  });

  async function fetchList(params: MerchantAgentListQuery) {
    loading.list = true;
    const response = await MerchantAgentAPI.getMerchantAgentList(params);
    loading.list = false;
    if (response) {
      list.value = response.records;
      totalCount.value = response.total_count;
      totalPage.value = response.total_page;
    }
  }

  async function fetchTransactionList(params: MerchantAgentTransactionListQuery) {
    loading.transactionList = true;
    const response = await MerchantAgentAPI.getMerchantAgentTransactionList(params);
    loading.transactionList = false;
    if (response) {
      transactionList.value = response.records;
      totalCount2.value = response.total_count;
      totalPage2.value = response.total_page;
    }
  }

  return {
    list,
    transactionList,
    totalCount2,
    totalPage2,
    totalCount,
    totalPage,
    loading,
    fetchList,
    fetchTransactionList,
  };
});

export const useMerchantAgentStoreHook = () => {
  return useMerchantAgentStore(store);
};
